﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArithmeticDemo
{
    /// <summary>
    /// 吉利算法面试
    /// </summary>
    class JiLi
    {
        public void Start() {
            var result1 = GetResult(9);
            var result = GetResult(125);
            Console.WriteLine(result1 + "分割" + result);
            var ss = string.Compare("bas", "abs");
            Console.WriteLine(ss);
            var resList = GetResult("bc", 3);
            Console.WriteLine("总数为：" + resList.Count);
            foreach (var item in resList) {
                Console.WriteLine(item);
            }
        }

        private  System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();

        private  List<string> GetResult(string sources_str, int max) {
            var builderList = new List<StringBuilder>();
            var strlenght = sources_str.Length;
            //var num = GetNum(sources_str);
            //var max = 96 + max;
            var sb = new StringBuilder();
            for (int i = 0; i < strlenght; i++) {
                sb = sb.Append((char)(97));
            }
            builderList.Add(sb);
            var begin = 0;
            if (strlenght % 2 == 0) {
                begin = strlenght / 2;
            } else if (strlenght != 1) {
                begin = (strlenght / 2) + 1;
            }
            EvenNumber(strlenght, sources_str, max, begin, builderList, sb);
            var result = new List<string>();
            foreach (var item in builderList) {
                if (!result.Contains(item.ToString())) {
                    result.Add(item.ToString());
                }
            }
            return result;
        }
        private  void EvenNumber(int strlenght, string sources_str, int max, int change_char, List<StringBuilder> builderList, StringBuilder itemstr) {
            var sbstr = new StringBuilder(itemstr.ToString());
            if (change_char < 0) {
                return;
            }
            EvenNumber(strlenght, sources_str, max, change_char - 1, builderList, sbstr);
            for (int i = 1; i < max; i++) {
                var new_char = (char)(97 + i);
                sbstr[change_char] = new_char;
                sbstr[strlenght - change_char - 1] = new_char;
                if (string.Compare(sources_str, sbstr.ToString()) > 0) {
                    builderList.Add(new StringBuilder(sbstr.ToString()));
                    EvenNumber(strlenght, sources_str, max, change_char - 1, builderList, sbstr);
                }


            }
        }

        private  int ToAscii(char s) {
            return (int)asciiEncoding.GetBytes(s.ToString())[0];
        }

        /// <summary>
        /// 数字
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        private  int GetResult(int N) {
            var S = 1;
            var result = 0;
            var isContinue = true;
            while (isContinue) {

                if (S == 1) {
                    result++;
                    S++;
                    continue;
                }
                var num = 0;
                if ((((float)N) / S) < (S / 2 + 1)) {
                    isContinue = false;
                }
                //偶数
                if (S % 2 == 0) {
                    num = (2 * (N / S) + 1) * (S / 2);
                }
                //奇数
                else {
                    num = (N / S) + (N / S) * (S - 1);
                }
                if (num == N) {
                    result++;
                }
                S++;

            }
            return result;
        }
    }
}
